<?php

namespace app\shop\controller;

use buildView\Button;
use think\Controller;
use buildView\Detail;
use buildView\Form;
use buildView\Grid;
use app\shop\model\ShopSpellGood;

/**
 * 拼团商品
 * Class SpellGood
 * @package app\shop\controller
 */
class SpellGood extends Controller
{
    /**
     * 活动图片
     * @auth true
     * @menu true
     * @Author: rocky
     * 2019/12/11 14:13
     */
    public function image()
    {
        $form = new Form();
        $form->setTitle('活动图片');
        $form->image('seckill_banner','秒杀活动图')->baseUrl(true);
        $form->image('spell_banner','拼团活动图')->baseUrl(true);
        $form->md(6);
        return $form->view();
    }

    /**
     * 列表
     * @auth true
     * @menu true
     * @return string
     */
    public function index()
    {
        $grid = new Grid(new ShopSpellGood());
        $grid->model()->with(['good' => function ($q) {
            $q->withSum('specs', 'stock')->withSum(['specs' => function ($q) {
                return 'sale_num';
            }], 'sale');
        }]);
        $grid->setTitle('拼团商品');
        $stock_warning = sysconf('stock_warning');
        $grid->logo('商品图片')->width(100)->image(5, 80, 80);
        $grid->column('info', '商品信息')->display(function ($val, $data) {
            $data = $data['good'];
            $html = "商品编号：{$data['id']}<br>商品名称：{$data['title']}";
            return $html;
        })->width(200)->align('left');

        $grid->good()->specs('<div class="layui-row" style="line-height:28px">
                    <div class="layui-col-xs6 text-left layui-elip">商品规格</div>
                    <div class="layui-col-xs3 text-center layui-elip">拼团价</div>
                    <div class="layui-col-xs3 text-center layui-elip">库存 / 销量 / 剩余库存</div>
                </div>')
            ->display(function ($specs, $data) use ($stock_warning) {
                $html = '<div class="layui-row" style="line-height:23px">
                        <div class="layui-col-xs6 text-left layui-elip font-s10">--</div>
                        <div class="layui-col-xs3 text-center layui-elip color-blue font-s10">--</div>
                        <div class="layui-col-xs3 text-center layui-elip color-blue font-s10">--</div>
                    </div>';
                if ($specs->isEmpty()) {
                    return $html;
                } else {
                    $html = '<div style="overflow:auto;max-height:68px;padding:5px 0">';
                }
                foreach ($specs as $spec) {
                    $stock = $spec['stock'] - $spec['sale'];
                    if ($stock <= $stock_warning) {
                        $stock = '<span class="color-red">' . $stock . '</span>';
                    }

                    $html .= ' <div class="layui-row" style="line-height:23px">
                        <div class="layui-col-xs6 text-left layui-elip font-s10">' . str_replace(['::', ';;'], [':', ';'], $spec['spec_name']) . '</div>
                        <div class="layui-col-xs3 text-center layui-elip color-blue font-s10">' . $spec['spell_price'] . '</div>
                        <div class="layui-col-xs3 text-center layui-elip color-blue font-s10">' . $spec['stock'] . ' / ' . $spec['sale'] . ' / ' . $stock . '</div>
                    </div>';
                }
                $html .= '</div>';
                return $html;
            })->minWidth(550)->align('left');
        $grid->good()->sepll_num('成团人数')->width(80);
        $grid->column('sale_num', '库存统计')->display(function ($val, $data) use ($stock_warning) {
            $data = $data['good'];
            $stock = $data['specs_sum'] ? $data['specs_sum'] : 0;
            $sale = $data['sale_num'] ? $data['sale_num'] : 0;
            $stock_num = $stock - $sale;
            if ($stock_num <= $stock_warning) {
                $stock_num = '<span class="color-red">剩余库存：' . $stock_num . '</span>';
            } else {
                $stock_num = '<span>剩余库存：' . $stock_num . '</span>';
            }
            return '库存：' . $stock . '<br>' . $stock_num . '<br>销量：<span class="layui-badge layui-bg-green">' . $sale . '</span>';
        })->width(150)->align('left');
        $grid->good()->price('价格')->display(function ($val, $data) {
            $data = $data['good'];
            return '价格：￥' . $data['price'] . '<br>运费：￥' . $data['price_express'];
        })->width(150)->align('left');
        $grid->hideExportButton();
        $grid->hideAddButton();
        $button = new Button('选择商品');
        $button->href(url('goods/index') . '?spell=true', 'iframe');
        $grid->addTools($button);
        $grid->actions(function ($action, $data) {
            $action->hideDetail();
            $action->hideEdit();
            $button = new Button('编辑', '', 'sm', 'layui-icon-edit');
            $button->href(url('goods/form') . '?back=1&id=' . $data['good_id']);
            $action->prepend($button);
        });
        return $grid->view();
    }

    public function add()
    {
        $good_ids = $this->request->put('select_ids');
        $good_idsArr = explode(',', $good_ids);
        $data = [];
        foreach ($good_idsArr as $val) {
            $data[] = [
                'good_id' => $val,
            ];
        }
        (new ShopSpellGood())->saveAll($data);
        $this->success('添加成功', '');
    }
}
